package com.member.shop.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.member.common.utils.DateUtils;
import com.member.common.utils.StringUtils;
import com.member.common.utils.bean.BeanUtils;
import com.member.shop.domain.SysBizActivity;
import com.member.shop.domain.SysBizActivityEnroll;
import com.member.shop.domain.SysBizUser;
import com.member.shop.dto.FyPayDto;
import com.member.shop.dto.WxPayDto;
import com.member.shop.mapper.SysBizActivityEnrollMapper;
import com.member.shop.service.IPayService;
import com.member.shop.service.ISysBizActivityEnrollService;
import com.member.shop.service.ISysBizActivityService;
import com.member.shop.service.ISysBizUserService;
import com.member.shop.vo.FieldData;
import com.member.shop.vo.PayVo;
import com.member.shop.vo.WriteOffDetailVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * 活动报名Service业务层处理
 *
 * @author lixinchen
 * @date 2024-01-09
 */
@Service
public class SysBizActivityEnrollServiceImpl extends ServiceImpl<SysBizActivityEnrollMapper, SysBizActivityEnroll> implements ISysBizActivityEnrollService {
    @Resource
    private SysBizActivityEnrollMapper sysBizActivityEnrollMapper;
    @Resource
    private ISysBizUserService sysBizUserService;
    @Resource
    private ISysBizActivityService sysBizActivityService;
    @Resource
    private IPayService payService;

    /**
     * 查询活动报名
     *
     * @param id 活动报名主键
     * @return 活动报名
     */
    @Override
    public SysBizActivityEnroll selectSysBizActivityEnrollById(Long id) {
        SysBizActivityEnroll sysBizActivityEnroll = sysBizActivityEnrollMapper.selectById(id);
        if (StringUtils.isNotBlank(sysBizActivityEnroll.getInfo())) {
            List<FieldData> fieldDataList = JSON.parseArray(sysBizActivityEnroll.getInfo(), FieldData.class);
            sysBizActivityEnroll.setFieldList(fieldDataList);
        }
        return sysBizActivityEnroll;
    }

    /**
     * 查询活动报名列表
     *
     * @param queryWrapper 活动报名
     * @return 活动报名
     */
    @Override
    public List<SysBizActivityEnroll> selectSysBizActivityEnrollList(LambdaQueryWrapper<SysBizActivityEnroll> queryWrapper) {
        return sysBizActivityEnrollMapper.selectList(queryWrapper);
    }

    /**
     * 查询活动报名列表
     *
     * @param sysBizActivityEnroll 活动报名
     * @return 活动报名
     */
    @Override
    public List<SysBizActivityEnroll> selectSysBizActivityEnrollList(SysBizActivityEnroll sysBizActivityEnroll) {
        return sysBizActivityEnrollMapper.selectSysBizActivityEnrollList(sysBizActivityEnroll);
    }

    /**
     * 新增活动报名
     *
     * @param sysBizActivityEnroll 活动报名
     * @return 结果
     */
    @Override
    public int insertSysBizActivityEnroll(SysBizActivityEnroll sysBizActivityEnroll) {
        sysBizActivityEnroll.setCreateTime(DateUtils.getNowDate());
        return sysBizActivityEnrollMapper.insert(sysBizActivityEnroll);
    }

    /**
     * 修改活动报名
     *
     * @param sysBizActivityEnroll 活动报名
     * @return 结果
     */
    @Override
    public int updateSysBizActivityEnroll(SysBizActivityEnroll sysBizActivityEnroll) {
        sysBizActivityEnroll.setUpdateTime(DateUtils.getNowDate());
        return sysBizActivityEnrollMapper.updateById(sysBizActivityEnroll);
    }

    /**
     * 批量删除活动报名
     *
     * @param ids 需要删除的活动报名主键
     * @return 结果
     */
    @Override
    public int deleteSysBizActivityEnrollByIds(Long[] ids) {
        return sysBizActivityEnrollMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除活动报名信息
     *
     * @param id 活动报名主键
     * @return 结果
     */
    @Override
    public int deleteSysBizActivityEnrollById(Long id) {
        return sysBizActivityEnrollMapper.deleteById(id);
    }

    @Override
    public String preSign(Long id, Long userId) {
        SysBizActivity sysBizActivity = sysBizActivityService.selectSysBizActivityById(id);
        if (sysBizActivity == null) {
            return "活动不存在";
        }
        if (sysBizActivity.getActivityEndTime().compareTo(DateUtils.getNowDate()) <= 0) {
            return "活动已结束";
        }
        List<SysBizActivityEnroll> sysBizActivityEnrolls = sysBizActivityEnrollMapper.selectList(new LambdaQueryWrapper<SysBizActivityEnroll>().eq(SysBizActivityEnroll::getUserId, userId).eq(SysBizActivityEnroll::getActivityId, id).eq(SysBizActivityEnroll::getPayStatus, 1).eq(SysBizActivityEnroll::getEnrollStatus, 0));
        if (CollectionUtils.isEmpty(sysBizActivityEnrolls)) {
            return "未报名活动";
        }
        return StringUtils.EMPTY;
    }

    @Override
    public String preDealerSign(Long id, Long userId) {
        return null;
    }

    @Override
    public WriteOffDetailVo queryWriteOffById(Long id, Long userId) {
        SysBizActivityEnroll sysBizActivityEnroll = sysBizActivityEnrollMapper.selectById(id);
        WriteOffDetailVo writeOffDetailVo = new WriteOffDetailVo();
        BeanUtils.copyProperties(sysBizActivityEnroll, writeOffDetailVo);
        SysBizUser sysBizUser = sysBizUserService.selectSysBizUserById(userId);
        writeOffDetailVo.setUserName(sysBizUser.getNickName());
        return writeOffDetailVo;
    }

    /**
     * 获取活动报名总金额
     */
    @Override
    public BigDecimal paymentAmount(SysBizActivityEnroll sysBizActivityEnroll) {
        return sysBizActivityEnrollMapper.paymentAmount(sysBizActivityEnroll);
    }

}
